package cn.liuhuan1.mongoDb.service;

import cn.liuhuan1.entity.ReturnResult;
import cn.liuhuan1.mongoDb.mongoDbImp.MongoDbImp;
import cn.liuhuan1.mongoDb.mongoUtile.MongoUtile;
import cn.liuhuan1.webModelBean.vo.ArticleCommentVo;
import com.mongodb.WriteResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Auther: liuhuan
 * @Date: 2021/4/19 - 04 - 19 - 19:29
 * @Description:
 * @version: 1.0
 */
@Service
public class MongoService implements MongoDbImp {

    public static final String COLLECTION_NAME = "articles";
    public static final String SON_OBJECT_ID = "0";
    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void inser(ArticleCommentVo articleCommentVo) {
        if(articleCommentVo.getSonObjectId() == null || articleCommentVo.getSonObjectId().trim() == ""){
            articleCommentVo.setSonObjectId(SON_OBJECT_ID);
        }else{
            articleCommentVo.setSonObjectId(MongoUtile.getObjectId(articleCommentVo.getSonObjectId()));
            Update update = new Update();
            Query query = new Query(Criteria.where("id").is(articleCommentVo.getSonObjectId()));
            update.inc("commentNumber",1);
            mongoTemplate.updateFirst(query,update,COLLECTION_NAME);
        }
        articleCommentVo.setStartTime(new Date());
        mongoTemplate.insert(articleCommentVo,COLLECTION_NAME);
    }

    @Override
    public void inserList(List<ArticleCommentVo> articleCommentVoList) {
        for (ArticleCommentVo articleCommentVo : articleCommentVoList) {
            if(null != articleCommentVo .getSonObjectId() && articleCommentVo.getSonObjectId().trim() != ""){
                articleCommentVo.setSonObjectId(MongoUtile.getObjectId(articleCommentVo .getSonObjectId()));
            }
        }
        mongoTemplate.insert(articleCommentVoList,COLLECTION_NAME);
    }

    @Override
    public List<ArticleCommentVo> findAll(String articleId) {
        Sort sort = new Sort(Sort.Direction.DESC,"startTime");
        Query query = new Query(Criteria.where("articleId").is(articleId).and("sonObjectId").is(SON_OBJECT_ID));
        List<ArticleCommentVo> articleCommentVos = mongoTemplate.find(query.with(sort), ArticleCommentVo.class, COLLECTION_NAME);
        for (ArticleCommentVo articleCommentVo : articleCommentVos) {
                articleCommentVo.setArticleCommentVos(findRecursion(articleCommentVo.getId()));
        }
        return articleCommentVos;
    }

    @Override
    public ReturnResult updateLikeNumber(String id, Integer likeNumber) {
        Query query = new Query(Criteria.where("id").is(id));
        Update update = new Update();
        update.inc("likeNumber",1);
        WriteResult likeNumber1 = mongoTemplate.updateFirst(query, update, COLLECTION_NAME);
        System.out.println("-------------------------");
        System.out.println(likeNumber1);
        System.out.println("-------------------------");

        return ReturnResult.succes();
    }

    private List<ArticleCommentVo> findRecursion(String sonObjectId){
        Sort startTime = new Sort(Sort.Direction.DESC, "startTime");
        Query query = new Query(Criteria.where("sonObjectId").is(sonObjectId));
        List<ArticleCommentVo> articleCommentVos = mongoTemplate.find(query.with(startTime), ArticleCommentVo.class, COLLECTION_NAME);
        for (ArticleCommentVo articleCommentVo : articleCommentVos) {
                articleCommentVo.setArticleCommentVos(findRecursion(articleCommentVo.getId()));
        }
        return articleCommentVos;
    }
}
